package com.settlement.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.common.annotation.PreventDuplicateSubmit;
import com.settlement.system.common.result.PageResult;
import com.settlement.system.common.result.Result;
import com.settlement.system.model.entity.OdsErpRetailSalesOrderDetails;
import com.settlement.system.service.OdsErpRetailSalesOrderDetailsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.settlement.system.model.query.OdsErpRetailSalesOrderDetailsQuery;
import com.settlement.system.model.form.OdsErpRetailSalesOrderDetailsForm;
import org.springdoc.core.annotations.ParameterObject;
import io.swagger.v3.oas.annotations.Parameter;
import com.settlement.system.model.vo.OdsErpRetailSalesOrderDetailsImportVo;
import com.settlement.system.listener.easyexcel.OdsErpRetailSalesOrderDetailsListener;
import com.alibaba.excel.EasyExcel;

import com.alibaba.excel.EasyExcelFactory;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 * ERP零售销货单明细表 前端控制器
 * </p>
 *
 * @author huangrensen
 * @since 2024-08-02
 */
@RestController
@RequestMapping("/api/v1/odsErpRetailSalesOrderDetails")
@RequiredArgsConstructor
public class OdsErpRetailSalesOrderDetailsController {

    private final OdsErpRetailSalesOrderDetailsService odsErpRetailSalesOrderDetailsService;

    @Operation(summary = "ERP零售销货单明细表分页", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/page")
    public PageResult<OdsErpRetailSalesOrderDetails> getPage(@ParameterObject OdsErpRetailSalesOrderDetailsQuery queryParams) {
        Page<OdsErpRetailSalesOrderDetails> result = odsErpRetailSalesOrderDetailsService.getPage(queryParams);
        return PageResult.success(result);
    }


    @Operation(summary = "保存ERP零售销货单明细表", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping
    @PreventDuplicateSubmit
    @PreAuthorize("@ss.hasPerm('sys:odsErpRetailSalesOrderDetails:save')")
    public Result<?> saveOdsErpRetailSalesOrderDetails(@RequestBody @Valid OdsErpRetailSalesOrderDetailsForm odsErpRetailSalesOrderDetailsForm) {
        boolean result = odsErpRetailSalesOrderDetailsService.saveOdsErpRetailSalesOrderDetails(odsErpRetailSalesOrderDetailsForm);
        return Result.judge(result);
    }

    @Operation(summary = "获取ERP零售销货单明细表信息", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/{id}/form")
    public Result<OdsErpRetailSalesOrderDetailsForm> getOdsErpRetailSalesOrderDetailsFormData(@Parameter(description = "主键ID") @PathVariable Long id) {
        OdsErpRetailSalesOrderDetailsForm odsErpRetailSalesOrderDetails = odsErpRetailSalesOrderDetailsService.getOdsErpRetailSalesOrderDetailsFormData(id);
        return Result.success(odsErpRetailSalesOrderDetails);
    }

    @Operation(summary = "删除ERP零售销货单明细表", security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:odsErpRetailSalesOrderDetails:delete')")
    public Result<?> deleteOdsErpRetailSalesOrderDetails(@Parameter(description = "ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = odsErpRetailSalesOrderDetailsService.deleteOdsErpRetailSalesOrderDetails(ids);
        return Result.judge(result);
    }

    @Operation(summary = "ERP零售销货单明细表导入", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/odsErpRetailSalesOrderDetailsImport")
    @PreAuthorize("@ss.hasPerm('sys:odsErpRetailSalesOrderDetails:import')")
    @PreventDuplicateSubmit
    public Result<?> odsErpRetailSalesOrderDetailsImport(@RequestParam("file") MultipartFile file) throws Exception {
        OdsErpRetailSalesOrderDetailsListener listener = new OdsErpRetailSalesOrderDetailsListener();
        EasyExcelFactory.read(file.getInputStream(), OdsErpRetailSalesOrderDetailsImportVo.class, listener).sheet(0).doRead();
        List<OdsErpRetailSalesOrderDetailsImportVo> list = listener.getVos();
        if (list == null || list.size() == 0) {
            return Result.success();
        }
        odsErpRetailSalesOrderDetailsService.importData(list);
        return Result.success();
    }

    @Operation(summary = "导出ERP零售销货单明细表", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/_export")
    @PreAuthorize("@ss.hasPerm('sys:odsErpRetailSalesOrderDetails:export')")
    @PreventDuplicateSubmit
    public void export(@RequestParam("ids") String ids, HttpServletResponse response) throws IOException {
        String fileName = "ERP零售销货单明细表.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
        List<OdsErpRetailSalesOrderDetailsImportVo> odsErpRetailSalesOrderDetailsList = odsErpRetailSalesOrderDetailsService.findByIds(ids);
        EasyExcel.write(response.getOutputStream(), OdsErpRetailSalesOrderDetailsImportVo.class).sheet("ERP零售销货单明细表").doWrite(odsErpRetailSalesOrderDetailsList);
    }
}

